package com.tool.cn.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 获取主机名
 * 
 * @author wangzh
 * 2017年7月27日 下午1:59:45
 */
public class HostNameUtil {
  
  
    public static String getHostName() {  
        if (System.getenv("COMPUTERNAME") != null) {  
            return System.getenv("COMPUTERNAME");  
        } else {  
        	try {  
                return (InetAddress.getLocalHost()).getHostName();  
            } catch (UnknownHostException uhe) {  
                String host = uhe.getMessage();
                if (host != null) {  
                    int colon = host.indexOf(':');  
                    if (colon > 0) {  
                        return host.substring(0, colon);  
                    }  
                }  
                return "UnknownHost";  
            }    
        }  
    }  

}
